package com.usian.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.common.constants.user.UserConstants;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.user.feign.ApAuthorFeign;
import com.usian.user.feign.WmUserFeign;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.mapper.ApUserRealnameMapper;
import com.usian.user.service.ApUserRealnameService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {

    @Autowired
    private ApAuthorFeign apAuthorFeign;

    @Autowired
    private WmUserFeign wmUserFeign;

    @Autowired
    private ApUserMapper apUserMapper;


    @Override
    public PageResponseResult loadListByStatus(AuthDto dto) {

        if (dto==null){
            return (PageResponseResult) PageResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }

        dto.checkParam();

        QueryWrapper<ApUserRealname> wrapper = new QueryWrapper<ApUserRealname>();

        if (dto.getStatus()!=null){
            wrapper.lambda().eq(ApUserRealname::getStatus,dto.getStatus());
        }

        Page page = new Page(dto.getPage(),dto.getSize());

        IPage result = page(page,wrapper);

        PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) result.getTotal());

        responseResult.setCode(0);

        responseResult.setData(result.getRecords());

        return responseResult;
    }

    @GlobalTransactional
    @Override
    public ResponseResult updateStatusById(AuthDto dto, Short status) {

        //检查参数
        if (dto==null || dto.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        if (checkStatus(status)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //修改状态
        ApUserRealname apUserRealname = new ApUserRealname();

        apUserRealname.setId(dto.getId());
        apUserRealname.setStatus(status);

        if (dto.getMsg()!=null){
            apUserRealname.setReason(dto.getMsg());
        }

        updateById(apUserRealname);

        //认证通过添加自媒体账号和作者账号
        if (status.equals(UserConstants.PASS_AUTH)){
            ResponseResult wmUserAndAuthor = createWmUserAndAuthor(dto);
            if (wmUserAndAuthor!=null){
                return wmUserAndAuthor;
            }
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /*
    * 创建自媒体账号以及作者账号
    * */
    public ResponseResult createWmUserAndAuthor(AuthDto dto){
        //查询ApUser信息
        ApUserRealname aur = getById(dto.getId());

        ApUser apUser = apUserMapper.selectById(aur.getUserId());

        if (apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 检测自媒体用户是否存在
        WmUser wmUser = wmUserFeign.findByName(apUser.getName());

        if (wmUser==null || wmUser.getId()==null){
            wmUser = new WmUser();
            wmUser.setApUserId(apUser.getId());
            wmUser.setCreatedTime(new Date());
            wmUser.setSalt(apUser.getSalt());
            wmUser.setName(apUser.getName());
            wmUser.setPassword(apUser.getPassword());
            wmUser.setStatus(9);
            wmUser.setPhone(apUser.getPhone());
            wmUserFeign.save(wmUser);
        }

        //创建作者账号
        createAuthor(wmUser);
        //修改apUser标记
        apUser.setFlag((short)1);
        apUserMapper.updateById(apUser);
        return null;
    }

    /*
    * 创建自媒体账号
    * */
    public void createAuthor(WmUser wmUser){


        ApAuthor apAuthor = apAuthorFeign.findByUserId(wmUser.getApUserId());
        int a = 1/0;
        if (apAuthor==null){
            apAuthor = new ApAuthor();
            apAuthor.setName(wmUser.getName());
            apAuthor.setType(Integer.valueOf(UserConstants.FAIL_AUTH));
            apAuthor.setCreatedTime(new Date());
            apAuthor.setUserId(wmUser.getApUserId());
            apAuthorFeign.add(apAuthor);
        }
    }

    /*
    * 状态检测
    * */
    public boolean checkStatus(Short status){

        if (status == null || (!status.equals(UserConstants.FAIL_AUTH) && !status.equals(UserConstants.PASS_AUTH))){
            return true;
        }
        return false;
    }
}
